﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JKF.BusinessLayer.BLL;
using JKF.Entities.Model;
using JKF.Substructure;
using OfficeWebUI.Common;
using OfficeWebUI.Ribbon;
using SFWS.XHandler.Core;
namespace JKF.Web
{
    public partial class Main : AWebPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            FillMenu();
        }

        protected void FillMenu()
        {
            bllSYS_MODULE sysModule = new bllSYS_MODULE();
            var sysModuleInfoList = sysModule.GetNowUser_Role_Module();
            var MenuLevel1 = sysModuleInfoList.Where(a => a.MODULE_CODE.Length == 3);
            RibbonTab ribbontab; // 2级
            RibbonGroup ribbongroup; // 3级
            GroupZone groupzone = new GroupZone(); // 4级
            foreach (var item in MenuLevel1.OrderBy(a => a.MODULE_CODE))
            {
                ribbontab = new RibbonTab();
                ribbontab.Text = item.MODULE_NAME;
                ribbontab.ID = item.MODULE_NAME;	// add by zhangyan
                RibbonContext1.Tabs.Add(ribbontab); //添加标题头上面RibbonContext1控件是顶级显示（标题）
                var MenuLevel2 = sysModuleInfoList.Where(a => a.MODULE_CODE.StartsWith(item.MODULE_CODE) && a.MODULE_CODE.Length == 6);
                foreach (var item2 in MenuLevel2.OrderBy(a => a.MODULE_CODE))  //a.DISPLAY_ORDER
                {
                    ribbongroup = new RibbonGroup();
                    ribbongroup.Text = item2.MODULE_NAME;
                    ribbontab.Groups.Add(ribbongroup);
                    var MenuLevel3 = sysModuleInfoList.Where(a => a.MODULE_CODE.Length == 9 && a.MODULE_CODE.StartsWith(item2.MODULE_CODE));
                    var index = 0;
                    foreach (var item3 in MenuLevel3.OrderBy(a => a.MODULE_CODE))
                    {
                        var control = GenItem(item3);
                        if (!string.IsNullOrEmpty(item3.URL_PIC_LARGE))
                        {
                            groupzone = new GroupZone();
                            ribbongroup.Zones.Add(groupzone);
                            index = -1;
                        }
                        //暂不知道此用意！！！
                        if (index % 3 == 0)
                        {
                            groupzone = new GroupZone();
                            ribbongroup.Zones.Add(groupzone);
                        }
                        groupzone.Content.Add(control);
                        index++;
                    }
                }
            }
        }

        protected string Account
        {
            get { return bllSYS_USER.CurrentUser.ACCOUNT; }
        }

        protected string ShopName
        {
            get
            {
                bllSHOP shop = new bllSHOP();
                if (string.IsNullOrEmpty(bllSYS_USER.CurrentUser.SHOP_CODE))
                    return "";
                SHOPinfo shopInfo = shop.GetModel(bllSYS_USER.CurrentUser.SHOP_CODE);

                if (shopInfo != null)
                    return shopInfo.SHOP_NAME.Length > 10 ? shopInfo.SHOP_NAME.Substring(0, 10) : shopInfo.SHOP_NAME;
                return "";
            }
        }

        public Control GenItem(SYS_MODULEinfo item3)
        {
            string url = string.IsNullOrEmpty(item3.URL) ? "#" : ResolveUrl(item3.URL);

            if (string.IsNullOrEmpty(item3.URL_PIC_LARGE))
            {
                //小图
                var mediumitem = new MediumItem();
                mediumitem.NavigateUrl = string.IsNullOrEmpty(item3.URL) ? "#" : ResolveUrl(item3.URL);
                mediumitem.Text = item3.MODULE_NAME;
                mediumitem.ImageUrl = ResolveUrl(item3.URL_PIC_SMALL);
                //mediumitem.ClientClick = "top.tree.location.href='" + mediumitem.NavigateUrl + "';return false;";
                mediumitem.ClientClick = "tabObjFac(this);return false;";
                mediumitem.Attributes.Add(new KeyValuePair<string, string>("title", item3.MODULE_NAME));
                mediumitem.Attributes.Add(new KeyValuePair<string, string>("href", url == "#" ? "" : string.Format("{0}{1}{2}", url, (url.IndexOf("?") != -1 ? "&titleName=" : "?titleName="), HttpUtility.UrlEncode(item3.MODULE_NAME))));
                return mediumitem;
            }
            else
            {
                var mediumitem = new LargeItem();
                mediumitem.NavigateUrl = string.IsNullOrEmpty(item3.URL) ? "#" : ResolveUrl(item3.URL);
                mediumitem.Text = item3.MODULE_NAME;
                mediumitem.ImageUrl = ResolveUrl(item3.URL_PIC_LARGE);
                mediumitem.ClientClick = "tabObjFac(this);return false;";
                //mediumitem.Attributes.Add("title", item3.MODULE_NAME);
                mediumitem.Attributes.Add(new KeyValuePair<string, string>("title", item3.MODULE_NAME));
                mediumitem.Attributes.Add(new KeyValuePair<string, string>("href", url == "#" ? "" : string.Format("{0}{1}{2}", url, (url.IndexOf("?") != -1 ? "&titleName=" : "?titleName="), HttpUtility.UrlEncode(item3.MODULE_NAME))));
                //mediumitem.ClientClick = "top.tree.location.href='" + mediumitem.NavigateUrl + "';return false;";
                return mediumitem;
            }
        }
    }
}